package com.yuanzong.wuliu.common.vo;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    // 默认
    public static final String SUCCESS = "SUCCESS";
    public static final String ERROR = "ERROR";

    // 50008: 无效的Token
    public static final String TOKEN_ILLEGAL = "50008";
    // 50012: Other clients logged  其他设备登陆
    public static final String OTHER_CLIENTS_LOGGED = "50012";

    @Getter
    @Setter
    private String code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> ApiResult<T> success() {
        return instance(SUCCESS, "操作成功", null);
    }

    public static <T> ApiResult<T> success(T data) {
        return instance(SUCCESS, "操作成功", data);
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return instance(SUCCESS, msg, data);
    }

    public static <T> ApiResult<T> successMsg(String msg) {
        return instance(SUCCESS, msg, null);
    }

    public static <T> ApiResult<T> error() {
        return instance(ERROR, "操作失败", null);
    }

    public static <T> ApiResult<T> error(T data) {
        return instance(ERROR, "操作失败", data);
    }

    public static <T> ApiResult<T> error(String code, String msg) {
        return instance(code, msg);
    }

    public static <T> ApiResult<T> errMsg(String msg) {
        return instance(ERROR, msg, null);
    }

    public static <T> ApiResult<T> instance(String code, String msg, T data) {
        ApiResult result = new ApiResult<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static ApiResult instance(String code, String msg) {
        ApiResult result = new ApiResult<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}